﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-10 12:14:28
// 负责人：Sushee
// ===================================================================
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using OF.Utility;
using OF.Component.Model;
using OF.Component.IDAL;
using System.Text;
namespace OF.Component.DAL
{
	#region  数据访问层
	/// <summary>
	/// 数据访问层
	/// </summary>	
	internal partial class BaseLocation_All:IBaseLocation_All
	{		
    
            //连接字符串
            public virtual string ConnectionStringKey
            {
                get
                {
                    return ConstantUtility.ConnectionEjautobaseKey;
                }
            }
        #region GetMaxID
        
    
      
            public int GetMaxID()
            {
                int nReturn=0;
                string szSql=@"SELECT ISNULL(MAX(locationId),0)+1
                               FROM [BaseLocation_All]";             
                Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
                DbCommand dbc = db.GetSqlStringCommand(szSql);
                try
                {
                    object o = db.ExecuteScalar(dbc);
                    if (o!=null)
                    {
                        nReturn = Convert.ToInt32(o);
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                return nReturn;               
            }
            
      
        
        #endregion
    
		#region Basic CRUD functions
         private BaseLocation_AllInfo GetByReader(IDataReader dr)
         {
            BaseLocation_AllInfo info = new BaseLocation_AllInfo();
            object obj;
            obj=dr["locationId"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.locationId = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["locationName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.locationName = obj.ToString();
            }
            obj=dr["locationShortName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.locationShortName = obj.ToString();
            }
            obj=dr["locationSpellName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.locationSpellName = obj.ToString();
            }
            obj=dr["LocationGeographyTypeID"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.LocationGeographyTypeID = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["cId"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.cId = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["cName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.cName = obj.ToString();
            }
            obj=dr["cSpellName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.cSpellName = obj.ToString();
            }
            obj=dr["Pid"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.Pid = ((obj)==DBNull.Value)?0:Convert.ToInt32(obj);
            }
            obj=dr["pName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.pName = obj.ToString();
            }
            obj=dr["pspellName"];
            if (obj != null && obj != DBNull.Value)
            {                    
                info.pspellName = obj.ToString();
            }
            return info;
         }
		public int Insert(BaseLocation_AllInfo info)
		{
        
        
            string strSql=@"INSERT INTO BaseLocation_All			
			(			
			[locationId],
			[locationName],
			[locationShortName],
			[locationSpellName],
			[LocationGeographyTypeID],
			[cId],
			[cName],
			[cSpellName],
			[Pid],
			[pName],
			[pspellName]
			)
			 VALUES
			(			
			@locationId,
			@locationName,
			@locationShortName,
			@locationSpellName,
			@LocationGeographyTypeID,
			@cId,
			@cName,
			@cSpellName,
			@Pid,
			@pName,
			@pspellName
			)";
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql);
            db.AddInParameter(dbc, "@locationId",DbType.Int32, info.locationId);
            db.AddInParameter(dbc, "@locationName",DbType.String, info.locationName);
            db.AddInParameter(dbc, "@locationShortName",DbType.String, info.locationShortName);
            db.AddInParameter(dbc, "@locationSpellName",DbType.String, info.locationSpellName);
            db.AddInParameter(dbc, "@LocationGeographyTypeID",DbType.Int32, info.LocationGeographyTypeID);
            db.AddInParameter(dbc, "@cId",DbType.Int32, info.cId);
            db.AddInParameter(dbc, "@cName",DbType.String, info.cName);
            db.AddInParameter(dbc, "@cSpellName",DbType.String, info.cSpellName);
            db.AddInParameter(dbc, "@Pid",DbType.Int32, info.Pid);
            db.AddInParameter(dbc, "@pName",DbType.String, info.pName);
            db.AddInParameter(dbc, "@pspellName",DbType.String, info.pspellName);

             
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
            return (int)db.GetParameterValue(dbc,"locationId");
         
		}
		
		
        public void Update(BaseLocation_AllInfo info)
		{
            StringBuilder sbSql=new StringBuilder("UPDATE BaseLocation_All SET ");
            sbSql.AppendLine("[locationName] = @locationName,");
                
            sbSql.AppendLine("[locationShortName] = @locationShortName,");
                
            sbSql.AppendLine("[locationSpellName] = @locationSpellName,");
                
            sbSql.AppendLine("[LocationGeographyTypeID] = @LocationGeographyTypeID,");
                
            sbSql.AppendLine("[cId] = @cId,");
                
            sbSql.AppendLine("[cName] = @cName,");
                
            sbSql.AppendLine("[cSpellName] = @cSpellName,");
                
            sbSql.AppendLine("[Pid] = @Pid,");
                
            sbSql.AppendLine("[pName] = @pName,");
                
            sbSql.AppendLine("[pspellName] = @pspellName");
                
            sbSql.Append(" where  [locationId] = @locationId");
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(sbSql.ToString());
            db.AddInParameter(dbc, "@locationId",DbType.Int32, info.locationId);
            db.AddInParameter(dbc, "@locationName",DbType.String, info.locationName);
            db.AddInParameter(dbc, "@locationShortName",DbType.String, info.locationShortName);
            db.AddInParameter(dbc, "@locationSpellName",DbType.String, info.locationSpellName);
            db.AddInParameter(dbc, "@LocationGeographyTypeID",DbType.Int32, info.LocationGeographyTypeID);
            db.AddInParameter(dbc, "@cId",DbType.Int32, info.cId);
            db.AddInParameter(dbc, "@cName",DbType.String, info.cName);
            db.AddInParameter(dbc, "@cSpellName",DbType.String, info.cSpellName);
            db.AddInParameter(dbc, "@Pid",DbType.Int32, info.Pid);
            db.AddInParameter(dbc, "@pName",DbType.String, info.pName);
            db.AddInParameter(dbc, "@pspellName",DbType.String, info.pspellName);
			
           
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
		}
		
        public void Delete(int locationId)
		{
			StringBuilder strSql = new StringBuilder();
            strSql.Append("DELETE FROM [BaseLocation_All]  ");
            strSql.Append("WHERE [locationId]=@locationId ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@locationId",DbType.Int32, locationId);
            try
            {
                db.ExecuteNonQuery(dbc);
            }
            catch
            {	
                throw;
            }
          
           
		}
        
         public IList<BaseLocation_AllInfo> Search(out int pageCount, int pageIndex, int pageSize, string where, string orderField, bool isDesc)
        {
            IList<BaseLocation_AllInfo> list = new List<BaseLocation_AllInfo>();
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetStoredProcCommand("sp_OF_Page");
            //设置参数
            db.AddOutParameter(dbc, "@pageCount", DbType.Int32, 4);
            db.AddInParameter(dbc, "@pageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbc, "@pageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbc, "@strWhere", DbType.String, where);
            db.AddInParameter(dbc, "@fldName", DbType.String, orderField);
            db.AddInParameter(dbc, "@OrderType", DbType.Boolean, isDesc);
            db.AddInParameter(dbc, "@tblName", DbType.String, "BaseLocation_All");
            db.AddInParameter(dbc, "@strGetFields", DbType.String, "*");
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                while (dr.Read())
                {
                    BaseLocation_AllInfo info = GetByReader(dr);
                    list.Add(info);
                }
            }
            pageCount = (int)db.GetParameterValue(dbc, "@PageCount");
            return list;
        }
        
      
        public BaseLocation_AllInfo GetByID(int locationId)
		{
            Model.BaseLocation_AllInfo  info = null;
		    StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT  * FROM [BaseLocation_All] ");
            strSql.Append("WHERE [locationId]=@locationId ");
           
            Database db = DatabaseFactory.CreateDatabase(this.ConnectionStringKey);
            DbCommand dbc = db.GetSqlStringCommand(strSql.ToString());
		    db.AddInParameter(dbc, "@locationId",DbType.Int32, locationId);
            using (IDataReader dr = db.ExecuteReader(dbc))
            {
                if (dr.Read())
                {
                    info = GetByReader(dr);
                }
            }
            return info;
		}
		
        
		#endregion
	}
	#endregion
}